All files / web/src/app/api/arcade/rooms/[roomId]/join-requests/[requestId]/deny route.ts

0% Statements 0/72
0% Branches 0/1
0% Functions 0/1
0% Lines 0/72

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73                                                                                                                                                 
import { eq } from 'drizzle-orm'
import { NextResponse } from 'next/server'
import { db, schema } from '@/db'
import { getRoomMembers } from '@/lib/arcade/room-membership'
import { denyJoinRequest } from '@/lib/arcade/room-join-requests'
import { withAuth } from '@/lib/auth/withAuth'
import { getUserId } from '@/lib/viewer'
import { getSocketIO } from '@/lib/socket-io'

/**
 * POST /api/arcade/rooms/:roomId/join-requests/:requestId/deny
 * Deny a join request (host only)
 */
export const POST = withAuth(async (_request, { params }) => {
  try {
    const { roomId, requestId } = (await params) as { roomId: string; requestId: string }
    const userId = await getUserId()

    // Check if user is the host
    const members = await getRoomMembers(roomId)
    const currentMember = members.find((m) => m.userId === userId)

    if (!currentMember) {
      return NextResponse.json({ error: 'You are not in this room' }, { status: 403 })
    }

    if (!currentMember.isCreator) {
      return NextResponse.json({ error: 'Only the host can deny join requests' }, { status: 403 })
    }

    // Get the request
    const [request] = await db
      .select()
      .from(schema.roomJoinRequests)
      .where(eq(schema.roomJoinRequests.id, requestId))
      .limit(1)

    if (!request) {
      return NextResponse.json({ error: 'Join request not found' }, { status: 404 })
    }

    if (request.status !== 'pending') {
      return NextResponse.json({ error: 'Join request is not pending' }, { status: 400 })
    }

    // Deny the request
    const deniedRequest = await denyJoinRequest(requestId, userId, currentMember.displayName)

    // Notify the requesting user via socket
    const io = await getSocketIO()
    if (io) {
      try {
        io.to(`user:${request.userId}`).emit('join-request-denied', {
          roomId,
          requestId,
          deniedBy: currentMember.displayName,
        })

        console.log(
          `[Deny Join Request API] Request ${requestId} denied for user ${request.userId} to join room ${roomId}`
        )
      } catch (socketError) {
        console.error('[Deny Join Request API] Failed to broadcast denial:', socketError)
      }
    }

    return NextResponse.json({ request: deniedRequest }, { status: 200 })
  } catch (error: any) {
    console.error('Failed to deny join request:', error)
    return NextResponse.json({ error: 'Failed to deny join request' }, { status: 500 })
  }
})